package com.cincc.cinccimdemo.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.cincc.cinccimdemo.R;
import com.cincc.cinccimdemo.common.Constants;
import com.cincc.cinccimdemo.common.ContactUtil;
import com.cincc.cinccimdemo.common.callback.OnItemClickListener;
import com.cincc.cinccimdemo.model.FriendInfoUI;
import com.cincc.cinccimdemo.model.GroupMemberInfoUI;
import com.cincc.im.model.dbmodel.FriendInfo;
import com.cincc.im.model.dbmodel.GroupMemberInfo;

import java.util.ArrayList;
import java.util.List;

public class RemoveGroupMemberAdapter extends RecyclerView.Adapter<RemoveGroupMemberAdapter.MyViewHolder> {
    Context mContext;
    private final List<GroupMemberInfoUI> mList = new ArrayList<>();
    private final List<GroupMemberInfoUI> tempList = new ArrayList<>();

    public RemoveGroupMemberAdapter(Context mContext) {
        this.mContext = mContext;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.list_contact_my_friend_item, parent, false);
        return new MyViewHolder(view);
    }

    public List<GroupMemberInfoUI> getList() {
        return mList;
    }

    public GroupMemberInfoUI getItem(int index) {
        return mList.get(index);
    }

    public void updateItem(int position) {
        GroupMemberInfoUI friendInfoUI = mList.get(position);
        if (!inCheck(friendInfoUI.memberId)) {
            mList.get(position).isChecked = !mList.get(position).isChecked;
            notifyItemChanged(position);
        }
    }

    public void removeItem(GroupMemberInfoUI friendInfoUI) {
        int index = 0;
        for (int i = 0; i < mList.size(); i++) {
            if (mList.get(i).memberId.equals(friendInfoUI.memberId)) {
                index = i;
                break;
            }
        }
        mList.get(index).isChecked = false;
        notifyItemChanged(index);
    }

    public void setContactList(List<GroupMemberInfo> conversationInfos) {
        this.mList.clear();
        List<GroupMemberInfoUI> temp = new ArrayList<>();
        for (GroupMemberInfo friendInfo : conversationInfos) {
            GroupMemberInfoUI item = ContactUtil.getGroupMemberInfoUI(friendInfo);
            if (inCheck(item.memberId)) {
                item.isChecked = true;
            }
            temp.add(item);
        }
        if (!temp.isEmpty()) {
            for (String letter : ContactUtil.LETTER_LIST) {
                List<GroupMemberInfoUI> beanList = ContactUtil.queryByGroupMemberHeader(temp, letter);
                this.mList.addAll(beanList);
            }
            notifyDataSetChanged();
        }
        tempList.addAll(mList);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {

        GroupMemberInfoUI model = mList.get(position);

        holder.nameText.setText(model.nickName);

        holder.firstTv.setVisibility(TextUtils.isEmpty(model.protraitUrl) ? View.VISIBLE : View.GONE);
        holder.avatar.setVisibility(TextUtils.isEmpty(model.protraitUrl) ? View.GONE : View.VISIBLE);
        if (TextUtils.isEmpty(model.protraitUrl)) {
            holder.firstTv.setText(model.nickName.substring(0, 1));
        } else {
            Glide.with(mContext).load(model.protraitUrl).placeholder(R.mipmap.avatar_def)
                    .transforms(new CenterCrop(), new RoundedCorners(15))
                    .into(holder.avatar);
        }

        if (position == ContactUtil.getPositionForSection(position, 1, mList)) {
            holder.headerTv.setVisibility(View.VISIBLE);
            holder.headerTv.setText(model.getLetterName());
        } else {
            holder.headerTv.setVisibility(View.GONE);
        }
        holder.itemView.setOnClickListener(v -> {
            if (!inCheck(model.memberId)) {
                iOnDataClickItem.onClick(position);
            }
        });
        holder.checkBox.setVisibility(View.VISIBLE);
        if (inCheck(model.memberId)) {
            holder.checkBox.setImageResource(R.mipmap.checkbox_checked_disable);
        } else {
            holder.checkBox.setImageResource(model.isChecked ? R.mipmap.checkbox_checked : R.mipmap.checkbox_normal_black);
        }
    }

    //判断是否是自己，进来后自己默认
    private Boolean inCheck(String memberId) {
        return memberId.equals(Constants.userInfo.userId);
    }

    private OnItemClickListener iOnDataClickItem;

    public void setOnDataClickItem(OnItemClickListener iOnClickItem) {
        this.iOnDataClickItem = iOnClickItem;
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {
        TextView firstTv;
        TextView nameText;
        TextView headerTv;
        ImageView avatar;
        ImageView checkBox;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            firstTv = itemView.findViewById(R.id.first_letter_tv);
            nameText = itemView.findViewById(R.id.mText_item_contact_name);
            headerTv = itemView.findViewById(R.id.mText_list_item_header);
            avatar = itemView.findViewById(R.id.img_avatar);
            checkBox = itemView.findViewById(R.id.checkbox);
        }
    }
}
